package com.android.calldelay;



import com.android.calldelay.alarmServices.SetAlarmService;

import android.R.string;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

public class CallDelayedAppWidgetProvider extends AppWidgetProvider {


	public static final String DEBUG_TAG = "MyProvider";
	public static AppWidgetManager appWidgetManager;
	public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
	public static String UPDATE_ACTION = "UpdateWidget";

	@Override
	public void onReceive(Context context, Intent intent) {

		super.onReceive(context, intent);
		if (intent.getAction().equals(ACTION_WIDGET_RECEIVER))

		{
			RemoteViews localRemoteViews = new RemoteViews(context.getPackageName(),R.layout.call_delayed_appwidget_layout);

			SharedPreferences sharedPreferences = context.getSharedPreferences(AppInfo.APP_PREF, 0);
			int totalDialTime = sharedPreferences.getInt(AppInfo.KEY_TOTAL_DIAL_TIME, 0);
			int totalAlertTime = sharedPreferences.getInt(AppInfo.KEY_TOTAL_ALERT_TIME, 0);
			int dialAttempts = sharedPreferences.getInt(AppInfo.KEY_DIAL_ATTEMPTS_KEY, 0);
			String firstNumber = sharedPreferences.getString(AppInfo.KEY_FIRST_NUMBER_KEY, "N/A");

			AppInfo.DIAL_ATTEMPTS = dialAttempts+1;
			SetAlarmService setAlarmService = new SetAlarmService(context);
			if (!firstNumber.equals("N/A")) {
				if (AppInfo.FLAG_ALARM_START) {
					setAlarmService.setupAlarm(true);
					setAlarmService.setupAlarm(totalDialTime-totalAlertTime);
					localRemoteViews.setViewVisibility(R.id.btn_widget_delayapp_start, View.GONE);
					localRemoteViews.setViewVisibility(R.id.textView_title, View.VISIBLE);
					localRemoteViews.setTextViewText(R.id.textView_title, firstNumber);
					ComponentName localComponentName = new ComponentName(context, CallDelayedAppWidgetProvider.class);
					AppWidgetManager.getInstance(context).updateAppWidget(localComponentName, localRemoteViews);

				}else {
					setAlarmService.setupAlarm(false);
					localRemoteViews.setViewVisibility(R.id.btn_widget_delayapp_start, View.VISIBLE);
					localRemoteViews.setViewVisibility(R.id.textView_title, View.GONE);
					ComponentName localComponentName = new ComponentName(context, CallDelayedAppWidgetProvider.class);
					AppWidgetManager.getInstance(context).updateAppWidget(localComponentName, localRemoteViews);
					;
				}

			} else {
				localRemoteViews.setTextViewText(R.id.textView_title, "Redial Off");
			}

		}
	}


	@Override
	public void onUpdate(Context paramContext, AppWidgetManager paramAppWidgetManager, int[] paramArrayOfInt)
	{

		// registering the click event 
		RemoteViews localRemoteViews = new RemoteViews(paramContext.getPackageName(), R.layout.call_delayed_appwidget_layout);
		Intent localIntent = new Intent(paramContext, CallDelayedAppWidgetProvider.class);
		localIntent.setAction(ACTION_WIDGET_RECEIVER);
		localRemoteViews.setOnClickPendingIntent(R.id.btn_widget_delayapp_start, PendingIntent.getBroadcast(paramContext, 0, localIntent, 0));
		localRemoteViews.setOnClickPendingIntent(R.id.textView_title, PendingIntent.getBroadcast(paramContext, 0, localIntent, 0));
		localRemoteViews.setViewVisibility(R.id.btn_widget_delayapp_start, View.VISIBLE);
		localRemoteViews.setViewVisibility(R.id.textView_title, View.GONE);
		//localRemoteViews.setTextViewText(R.id.textView_title, "Redial Off");
		//Toast.makeText(paramContext, "updated", Toast.LENGTH_SHORT).show();


		while (true)
		{
			paramAppWidgetManager.updateAppWidget(paramArrayOfInt, localRemoteViews);
			return;
		}

	}

}
